//
//  ChannelBlockView.m
//  RssReader
//
//  Created by Wang Shuguang on 12-4-25.
//  Copyright 2012 Dawn. All rights reserved.
//

#import "ChannelBlockView.h"
#import "UIImageView+WebCache.h"
#import "Rss.h"

@implementation ChannelBlockView
@synthesize channelIndex;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        //背景
		bgView = [UIButton buttonWithType:UIButtonTypeCustom];
		bgView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
		[bgView addTarget:self action:@selector(channelTouchDown:) forControlEvents:UIControlEventTouchDown];
		[bgView addTarget:self action:@selector(channelTouchUp:) forControlEvents:UIControlEventTouchUpInside];
		[bgView addTarget:self action:@selector(channelTouchUp:) forControlEvents:UIControlEventTouchUpOutside];
		[self addSubview:bgView];
		
		//ico
		icoView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 20, self.frame.size.width-60, self.frame.size.height-60)];
		icoView.backgroundColor = [UIColor clearColor];
		[self addSubview:icoView];
		[icoView release];
		
		//文字
		titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-40, self.frame.size.width, 40)];
		titleLabel.font = [UIFont systemFontOfSize:20.0f];
		titleLabel.textColor = [UIColor whiteColor];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.textAlignment = UITextAlignmentCenter;
		[self addSubview:titleLabel];
		[titleLabel release];
		
    }
    return self;
}

-(void) setChannelIndex:(NSInteger)index{
    channelIndex = index;
    Rss *rss = [Rss shareRss];
	NSDictionary *channel = [rss.channels objectAtIndex:index];
	
	if ([channel objectForKey:@"PhotoUrl"]!=[NSNull null] && STRINGHASVALUE([channel objectForKey:@"PhotoUrl"])) {
		[icoView setImageWithURL:[channel objectForKey:@"PhotoUrl"]];
	}else {
		[icoView setHidden:YES];
		titleLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
	}
	
	titleLabel.text = [channel objectForKey:@"Title"];
	NSDictionary *colorDict = [channel objectForKey:@"BgColor"];
	bgView.alpha = 0.95;
	bgView.backgroundColor = [UIColor colorWithRed:[[colorDict objectForKey:@"Red"] floatValue] 
											 green:[[colorDict objectForKey:@"Green"] floatValue] 
											  blue:[[colorDict objectForKey:@"Blue"] floatValue] alpha:[[colorDict objectForKey:@"Alpha"] floatValue]];
}

-(void) channelTouchDown:(id)sender{
	bgView.alpha = 0.9f;
}
-(void) channelTouchUp:(id)sender{
	bgView.alpha = 0.8;
    Rss *rss = [Rss shareRss];
	NSDictionary *channel = [rss.channels objectAtIndex:channelIndex];
	if ([delegate respondsToSelector:@selector(channelView:didSelectedWithUrl:)]) {
		[delegate channelView:self didSelectedWithUrl:[channel objectForKey:@"Url"]];
	}
}

- (void)dealloc {
    [super dealloc];
}




@end
